package com.cgy.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.cgy.entity.Computer;
import com.cgy.entity.ZiYuan;
import com.cgy.service.ComputerService;

import util.FenyeUtil;
import util.LayuiJsonUtils;
import util.Result;

@RestController
@RequestMapping("computer")
@CrossOrigin
public class ComputerController {
@Autowired
private ComputerService computerService;
	@GetMapping
	public LayuiJsonUtils<Computer> findAllConputerByName(LayuiJsonUtils<Computer> lj,Computer c,Integer zid){
		Pageable pageable = PageRequest.of(lj.getPage(), lj.getLimit());
		Page<Computer> computer = computerService.findBynameConputer(c, pageable, zid);
				
		lj.setData(computer.getContent());
		lj.setCount(computer.getTotalElements());
		return lj;
	} 
	@RequestMapping("findAllByZid")
	public FenyeUtil<Computer> findAllbyZid(FenyeUtil<Computer> fy,Computer c,Integer zid){
		System.out.println("我进入了根据zid查询的方法"+zid);
		Pageable pageable = PageRequest.of(fy.getPage(), fy.getLimit());
		System.out.println(zid+"zid");
		Page<Computer> computer = computerService.findBynameConputer(c, pageable, zid);
		fy.setData(computer.getContent());
		fy.setCount(computer.getTotalElements());
		return fy;
	}
	/*
	 * //点击机房查询该机房的电脑
	 * 
	 * @RequestMapping("findAllByZid") public FenyeUtil<Computer>
	 * findAllByZid(FenyeUtil<Computer> fy,Integer zid){
	 * System.out.println("我进入了根据资源室id查询资源的方法"+zid); Pageable pageable =
	 * PageRequest.of(fy.getPage(), fy.getSize()); Page<Computer> c1 =
	 * computerService.findAllByZid(zid); System.out.println(c1);
	 * fy.setData(c1.getContent()); fy.setCount(c1.getNumberOfElements()); return
	 * fy; }
	 */
	
	
	//添加电脑
	@PostMapping
	public Computer addComputer(Computer c,Integer zid) {
		ZiYuan z= new ZiYuan();
		z.setZid(zid);
		c.setZiYuan(z);
		return computerService.addAndUpdateConputer(c); 
	}
	//修改电脑信息
	@PutMapping
	public Computer upDateComputer(Computer c,Integer zid) {
		ZiYuan z= new ZiYuan();
		z.setZid(zid);
		c.setZiYuan(z);
		return computerService.addAndUpdateConputer(c);
	}
	//删除电脑
	@DeleteMapping("{cid}")
	public Integer deleteComputer(@PathVariable("cid") Integer cid) {
		return computerService.deleteConputer(cid);
	}
	
	//根据资源室id查询资源
	@RequestMapping("Byzid")
	public Object findAllByZid(HttpServletRequest request) {
		Result res = new Result();
		Map<String, Object> map = new HashMap<String, Object>();
		String page = request.getParameter("page")==null?"1":request.getParameter("page");
		String limit = request.getParameter("limit")==null?"10":request.getParameter("limit");
		map.put("page", (Integer.parseInt(page)-1)*Integer.parseInt(limit));
		map.put("limit", Integer.parseInt(limit));
		String zid = request.getParameter("zid");
		System.out.println("我进入了根据资源室id查询资源的方法+"+zid);
			map.put("zid", zid);
		System.out.println(map.get(zid));
		List<Computer> cList= computerService.findAllByzid(map);
		System.out.println(cList);
		Integer cCount = computerService.findAllByzidCount(map);
		System.out.println(cCount);
		res.setCode("0"); 
		res.setCount(cCount);
		res.setData(cList);
		return res;
	}
}
